package com.zeen.wima.proxy;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;

import com.zeen.wima.common.Ad;
import com.zeen.wima.common.GeoPoint;
import com.zeen.wima.common.HttpCallback;
import com.zeen.wima.common.Wima;

public class TraceProxy extends ProxyBase {

	public static String CLASS = "com.zeen.wima.proxy.TraceProxy";

	public final static int REGISTED = 0;
	public final static int SYNCD = 1;
	public final static int INTERNAL_ERR = 2;

	public TraceProxy() {
		super();
	}

	public void logPosition(String uid, GeoPoint p, int sensor) {
		if (p == null)
			return;
		ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
		param.add(new BasicNameValuePair("devId", uid));
		param.add(new BasicNameValuePair("lat", String.valueOf(p.getLat())));
		param.add(new BasicNameValuePair("lng", String.valueOf(p.getLng())));
		param.add(new BasicNameValuePair("sensor", String.valueOf(sensor)));
		sendRequest(url, "add_ckPoint", param, "", new HttpCallback() {
			@Override
			public void onResponse(String state, String msg, JSONArray data) {
				if (msg.equals("rs_adv")) {
					try {
						Wima.getInstance().setLastAd(
								new Ad(data.getString(0), data.getString(1)));
					} catch (Exception e) {

					}
				}
			}
		});
	}

	public void setPwd(String uid, String loginName, String loginPwd) {
		ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
		param.add(new BasicNameValuePair("devId", uid));
		param.add(new BasicNameValuePair("loginName", loginName));
		param.add(new BasicNameValuePair("loginPwd", loginPwd));
		sendRequest(url, "set_pwd", param, "", new HttpCallback() {
			@Override
			public void onResponse(String state, String msg, JSONArray data) {
				if (state.equals("rs_set_pwd")) {
					notifyObservers(REGISTED, msg);
				} else {
					notifyObservers(INTERNAL_ERR, null);
				}
			}
		});
	}
}
